package javagprssrv.Listener;

import javagprssrv.Log;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class TrdControlTranser_UDP extends java.lang.Thread {

    static final int MAX_PACKET_SIZE = 65507;
    private java.net.DatagramSocket socket;
    public java.lang.String RemoteIP;
    public int RemotePort;

    public TrdControlTranser_UDP(final java.net.DatagramSocket _socket) {
        super("TrdControlTranser_UDP");
        this.socket = _socket;
    }

    @java.lang.Override
    public void run() {
        byte[] buffer = new byte[65507];
        DatagramPacket data = new DatagramPacket(buffer, buffer.length);
        try {
            for (;;) {
                this.socket.receive(data);

                this.RemoteIP = data.getAddress().getHostAddress();
                this.RemotePort = data.getPort();

                byte[] r = new byte[data.getLength()];
                System.arraycopy(data.getData(), 0, r, 0, r.length);
                String recv = new String(r);

                String s = "[P=UDP,T=GET,S=";
                s = s + data.getAddress().getHostAddress() + ":";
                s = s + Integer.valueOf(data.getPort()).toString().trim()
                        + "] ";
                new Log().ShowLog(s + new Log().String2HexString(recv, " "),
                        Boolean.valueOf(true), Boolean.valueOf(true));
                if (recv.equals("exit")) {
                    this.socket.close();
                    break;
                }
            }
        } catch (Exception e) {
            new Log().ShowLog(e.toString(), Boolean.valueOf(true),
                    Boolean.valueOf(true));
        }
    }
}
